package cn.yangliu.nacos.comm.tools;

import java.util.Objects;

import cn.yangliu.nacos.comm.JsonResult;
import cn.yangliu.nacos.comm.enums.GlobalResultCode;
import cn.yangliu.nacos.comm.ex.GlobalException;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2019 -12-30
 */
public class ResponseUtils {

    /**
     * Instantiates a new Response utils.
     * forbid other class to create a new instance.
     * only itself can create a new instance.
     */
    private ResponseUtils() {

    }

    /**
     * Check result t.
     *
     * @param <T>  the type parameter
     * @param data the data
     * @return the t
     */
    public static <T> T checkResult(JsonResult<T> data){
        if (Objects.isNull(data)) {
            throw new GlobalException(GlobalResultCode.FAILD);
        }
        if (data.getCode() != 200) {
            throw new GlobalException(data.getCode(), data.getMsg());
        }
        return data.getData();
    }
}
